package com.tom.architect.day09.navigationbar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

/**
 * 导航栏的基类
 */
public class AbsNavigationBar<B extends AbsNavigationBar.Builder> implements INavigation {
    public B mBuilder;
    private View mNavigationBar;

    protected AbsNavigationBar(B builder) {
        this.mBuilder = builder;
        createNavigationBar();
    }

    @Override
    public void createNavigationBar() {
        mNavigationBar = LayoutInflater.from(mBuilder.mContext)
                .inflate(mBuilder.mLayoutId, mBuilder.mParent, false);
        // 添加View
        attachParent(mNavigationBar, mBuilder.mParent);
        // 绑定参数到View
        attachNavigationParams();
    }

    // 绑定参数
    @Override
    public void attachNavigationParams() {
        // 遍历所有文本。
        Map<Integer, CharSequence> textMaps = mBuilder.mTextMaps;
        for (Map.Entry<Integer, CharSequence> entry : textMaps.entrySet()) {
            TextView textView = findViewById(entry.getKey());
            textView.setText(entry.getValue());
        }
        // 设置点击事件。
        Map<Integer, View.OnClickListener> clickMaps = mBuilder.mClickListenerMaps;
        for (Map.Entry<Integer, View.OnClickListener> entry : clickMaps.entrySet()) {
            View view = findViewById(entry.getKey());
            view.setOnClickListener(entry.getValue());
        }
    }

    protected <T extends View> T findViewById(int viewId) {
        return mNavigationBar.findViewById(viewId);
    }

    // navigationBar添加到父布局
    @Override
    public void attachParent(View navigationBar, ViewGroup parent) {
        parent.addView(navigationBar, 0);
    }

    public B getBuilder() {
        return mBuilder;
    }

    // Builder构建类：构建构建NavigationBar，存储参数。泛型。 <B extends Builder>
    public static abstract class Builder<B extends Builder> {
        public Context mContext;
        public int mLayoutId;
        public ViewGroup mParent;

        public Map<Integer, CharSequence> mTextMaps;
        public Map<Integer, View.OnClickListener> mClickListenerMaps;

        public Builder(Context context, int layoutId, ViewGroup parent) {
            this.mContext = context;
            this.mLayoutId = layoutId;
            this.mParent = parent;

            this.mTextMaps = new HashMap<>();
            this.mClickListenerMaps = new HashMap<>();
        }

        // 创建NavigationBar, 抽象方法。
        public abstract AbsNavigationBar create();

        // 基类设置数据。返回的是AbsNavigationBar的Builder，但是当我们调用create()会报错。
        // 要让返回的 B，还是子类的Builder，要用到泛型。
        public B setText(int viewId, String text) {
            mTextMaps.put(viewId, text);
            return (B) this; // 返回的就是NavigationBar
        }

        // 泛型的使用。设置点击事件。
        public B setOnClickListener(int viewId, View.OnClickListener clickListener) {
            mClickListenerMaps.put(viewId, clickListener);
            return (B) this;
        }
    }
}
